package com.itheima.utils;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class SMSUtils {
    private static String secretId = null;
    private static String secretKey = null;
    private static Credential cred;
    private static HttpProfile httpProfile;
    private static ClientProfile clientProfile;
    private static String TemplateID = null;
    private static String SmsSdkAppid = null;
    private static String Sign = null;

    static {
        InputStream resourceAsStream = SMSUtils.class.getClassLoader().getResourceAsStream("SMS.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
            secretId = properties.getProperty("secretId");
            secretKey = properties.getProperty("secretKey");
            cred = new Credential(secretId, secretKey);
            httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            TemplateID = properties.getProperty("TemplateID");
            SmsSdkAppid = properties.getProperty("SmsSdkAppid");
            Sign = properties.getProperty("Sign");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                resourceAsStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void sendSMS(String phoneNumber, String code) throws TencentCloudSDKException {
        sendSMS(phoneNumber, code, "5");

    }

    public static void sendSMS(String PhoneNumber, String code, String minutes) throws TencentCloudSDKException {

        SmsClient client = new SmsClient(cred, "", clientProfile);
        Params params = new Params();
        params.setTemplateID(TemplateID);
        params.setSmsSdkAppid(SmsSdkAppid);
        params.setSign(Sign);

        params.setPhoneNumberSet(PhoneNumber);
        params.setTemplateParamSet(code, minutes);
        String paramsJson = JSONObject.toJSONString(params);

        SendSmsRequest req = SendSmsRequest.fromJsonString(paramsJson, SendSmsRequest.class);

        SendSmsResponse resp = null;

        resp = client.SendSms(req);


        System.out.println(SendSmsRequest.toJsonString(resp));

    }

    static class Params {
        @JSONField(name = "TemplateID")
        private String TemplateID;

        @JSONField(name = "SmsSdkAppid")
        private String SmsSdkAppid;

        @JSONField(name = "Sign")
        private String Sign;

        @JSONField(name = "PhoneNumberSet")
        private String[] PhoneNumberSet;


        @JSONField(name = "TemplateParamSet")
        private String[] TemplateParamSet;

        public String getSign() {
            return Sign;
        }

        public void setSign(String sign) {
            Sign = sign;
        }

        public String[] getPhoneNumberSet() {
            return PhoneNumberSet;
        }

        public void setPhoneNumberSet(String... phoneNumberSet) {
            PhoneNumberSet = phoneNumberSet;
        }

        public String getTemplateID() {
            return TemplateID;
        }

        public void setTemplateID(String templateID) {
            TemplateID = templateID;
        }

        public String[] getTemplateParamSet() {
            return TemplateParamSet;
        }

        public void setTemplateParamSet(String... templateParamSet) {
            TemplateParamSet = templateParamSet;
        }

        public String getSmsSdkAppid() {
            return SmsSdkAppid;
        }

        public void setSmsSdkAppid(String smsSdkAppid) {
            SmsSdkAppid = smsSdkAppid;
        }
    }

}

